<?php defined('SYSPATH') OR die('No direct access allowed.');

class Access_Controller extends Crud_Controller {

	public function __construct(){
		parent::__construct(); // This must be included
	}

	public function index(){
		$this->create_view('access/index');
		$this->view->render(TRUE);
	}
	
	public function listar(){
		$limit = $this->itens_por_pagina;
		$offset = 0;
		//it will return 10 records started from row #0
		$accesss = ORM::factory('access_log')->find_all($limit,$offset);
		$this->create_view('access/list');
		$this->view->accesss = $accesss;
		$this->view->cabecalho->title .= '::Listar';
		$this->view->render(TRUE);
		//var_dump($users);
	}

	public function criar(){

	}

	public function salvar(){

	}

	public function editar(){

	}

	public function deletar(){

	}

	public function ver(){

	}

	public function visitors($start_date = NULL, $end_date = NULL){
		$start_date = strtotime($this->input->get('start_date', date('m/d/Y g:i A', (time()-2592000))));
		$end_date = strtotime($this->input->get('end_date', date('m/d/Y g:i A', (time()))));

		$this->create_view('access/visitors');
		$this->view->logs = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));
		$this->view->table = Auto_Modeler_ORM::factory('access_log')->visitors($start_date, $end_date);
		$this->view->uniquetable = Auto_Modeler_ORM::factory('access_log')->unique_visitors($start_date, $end_date);

		// Get the general visits
		$data = Auto_Modeler_ORM::factory('access_log')->visitors($start_date, $end_date);
		$dataset = new Flot_Dataset();

		foreach ($data as $date => $visits)	{
			$dataset->add($date*1000, count($visits));
		}

		$flot = new Flot('visitorsgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->selection = array('mode' => 'x');

		$flot->add($dataset, 'Visits');

		$this->view->visits = $flot;

		// Find unique visitors
		$data = Auto_Modeler_ORM::factory('access_log')->unique_visitors($start_date, $end_date);
		$dataset = new Flot_Dataset();

		foreach ($data as $date => $visits)	{
			$dataset->add($date*1000, count($visits));
		}

		$flot = new Flot('uniquevisitorsgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->selection = array('mode' => 'x');

		$flot->add($dataset, 'Unique Visits');

		$this->view->unique_visits = $flot;
		$this->view->start_date = $start_date;
		$this->view->end_date = $end_date;
		$this->view->render(TRUE);
	}
	
	public function traffic_sources($start_date = NULL, $end_date = NULL){
		$start_date = strtotime($this->input->get('start_date', date('m/d/Y g:i A', (time()-2592000))));
		$end_date = strtotime($this->input->get('end_date', date('m/d/Y g:i A', (time()))));

		$data = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));

		$this->create_view('access/traffic_sources');
		$this->view->logs = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));

		$datasets = array();
		$files = array();
		$top_sources = array();
		$count = 0; // Only do 10
		foreach ($data as $visit){
			$files[$visit->referrer][strtotime(date('m/d/Y', $visit->date))][] = $visit;
			$top_sources[$visit->referrer] = isset($top_sources[$visit->referrer]) ? $top_sources[$visit->referrer] + 1 : 1;
		}

		arsort($top_sources);

		foreach ($files as $referrer => $files)	{
			if ($count++ > 10)
				break;
			// Make sure that all the dates are filled in
			for ($date = $start_date; $date <= $end_date; $date+=86400)	{
				if ( ! isset($files[strtotime(date('m/d/Y', $date))]))
					$files[strtotime(date('m/d/Y', $date))] = array();
			}
			ksort($files);
			$dataset = new Flot_Dataset;
			foreach ($files as $date => $foobar){
				//echo Kohana::debug($foobar);
				$dataset->add($date*1000, count($foobar));
			}
			$datasets[$referrer] = $dataset;
		}

		$flot = new Flot('contentgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%y/%m/%d');
		$flot->legend = array('position' => 'sw');

		foreach ($datasets as $name => $dataset)
			$flot->add($dataset, $name);

		$this->view->referrers = $flot;
		$this->view->top_referrers = $top_sources;
		$this->view->start_date = $start_date;
		$this->view->end_date = $end_date;
		$this->view->render(TRUE);
	}

	public function content(){
		$start_date = strtotime($this->input->get('start_date', date('m/d/Y g:i A', (time()-2592000))));
		$end_date = strtotime($this->input->get('end_date', date('m/d/Y g:i A', (time()))));

		$data = Auto_Modeler_ORM::factory('access_log')->fetch_where(array('date >=' => $start_date, 'date <' => $end_date));

		$this->create_view('access/content');
		$this->view->logs = $data;

		$datasets = array();
		$files = array();
		$top_content = array();
		$count = 0; // Only do 10
		foreach ($data as $visit){
			$files[$visit->file][strtotime(date('m/d/Y', $visit->date))][] = $visit;
			$top_content[$visit->file] = isset($top_content[$visit->file]) ? $top_content[$visit->file] + 1 : 1;
		}

		arsort($top_content);

		foreach ($files as $file => $files)	{
			if ($count++ > 10)
				break;
			// Make sure that all the dates are filled in
			for ($date = $start_date; $date <= $end_date; $date+=86400)	{
				if ( ! isset($files[strtotime(date('m/d/Y', $date))]))
					$files[strtotime(date('m/d/Y', $date))] = array();
			}
			ksort($files);
			$dataset = new Flot_Dataset;
			foreach ($files as $date => $foobar){
				//echo Kohana::debug($foobar);
				$dataset->add($date*1000, count($foobar));
			}
			$datasets[$file] = $dataset;
		}

		$flot = new Flot('contentgraph', array('style' => 'width: 800px; height: 400px;'));
		$flot->xaxis = array('mode' => 'time', 'timeformat' => '%d/%m');
		$flot->legend = array('position' => 'sw');

		foreach ($datasets as $name => $dataset)
			$flot->add($dataset, $name);

		$this->view->page_content = $flot;
		$this->view->top_content = $top_content;
		$this->view->start_date = $start_date;
		$this->view->end_date = $end_date;
		$this->view->render(TRUE);
	}

} // End Welcome Controller